package com.kasax.imgloader.demo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;

import com.kasax.imgloader.demo.adapter.HomeListAdapter;
import com.kasax.imgloader.demo.bean.SubListInfo;
import com.kasax.imgloader.demo.utils.LogUtils;
import com.kasax.imgloader.demo.utils.RetofitHelper;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private List<SubListInfo.DataBean> list = new ArrayList<>();
    private HomeListAdapter adapter;
    private Handler handler = new Handler(Looper.getMainLooper());

    @SuppressLint("CheckResult")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = findViewById(R.id.recyclerview);
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));

        adapter = new HomeListAdapter(this, list);
        recyclerView.setAdapter(adapter);
        loadData();
    }

    private void loadData() {
        RetofitHelper.createApiService()
                .getSublist()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        new Consumer<SubListInfo>() {

                            @Override
                            public void accept(SubListInfo subListInfo) throws Exception {
                                LogUtils.i(getClass(), "accept subListInfo: " + subListInfo);
                                handler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        adapter.updateList(subListInfo.getData());
                                    }
                                });

                            }
                        },
                        new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                LogUtils.e(getClass(), throwable);
                            }
                        });
//        RetofitHelper.createApiService()
//                .getBanners()
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(
//                        new Consumer<BannerResult>() {
//
//                            @Override
//                            public void accept(BannerResult bannerResult) throws Exception {
//                                LogUtils.i(getClass(), "accept subListInfo: " + bannerResult);
//                            }
//                        },
//                        new Consumer<Throwable>() {
//                            @Override
//                            public void accept(Throwable throwable) throws Exception {
//                                LogUtils.e(getClass(), throwable);
//                            }
//                        });
    }
}